﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.Diagnostics;
using System.Security;

namespace ShareMemory
{

    public static partial class Server
    {
        
        //Windows Message:
        private static IntPtr mappingHandle;
        private static IntPtr fileHandle;

        /// <summary>Creating a ShareMemory Mapping
        /// </summary>
        /// <param name="MappingFileName"></param>
        /// <returns>Handle of the ShareMemory</returns>
        public static IntPtr CreatShareMemory(string MappingFileName)
        {
            fileHandle = WinAPI.FileMapping.CreateFileMapping(IntPtr.Zero, IntPtr.Zero, (int)(WinAPI.FileMapping.MapProtection.PageReadWrite | WinAPI.FileMapping.MapProtection.SecCommit), 0, 1000000, MappingFileName);
            mappingHandle = WinAPI.FileMapping.MapViewOfFile(fileHandle, (int)WinAPI.FileMapping.MapAccess.FileMapWrite, 0, 0, new IntPtr(1024));
            return mappingHandle;
        }

        public static void WriteMapping(string message, IntPtr mappingHandle)
        {
            byte[] source = Encoding.ASCII.GetBytes(message);
            byte[] msg = new byte[1024];
            bool bRes;
           // General.LogFunc.LogWriteInDay(mappingHandle.ToString());
            Array.Copy(source, msg, source.Length);
            Marshal.Copy(msg, 0, mappingHandle, msg.Length);
            
            bRes=WinAPI.FileMapping.FlushViewOfFile(mappingHandle, new IntPtr(1024));
           // General.LogFunc.LogWriteInDay("WriteMapping result: "+ bRes .ToString ());
        }

        public static void ReleaseShareMem()
        {
            if (mappingHandle != IntPtr.Zero)
            {
                WinAPI.FileMapping.UnmapViewOfFile(mappingHandle);
                WinAPI.Process.CloseHandle(mappingHandle);
            }
            if (fileHandle != IntPtr.Zero)
                WinAPI.Process.CloseHandle(fileHandle);
        }
    }

    public static partial class Client
    {
        private static IntPtr mappingHandle;
        private static IntPtr fileHandle;

        public static string GetMessageFromShareMem(string MappingFileName)
        {
            byte[] msg = new byte[1024];
            string message;

            mappingHandle = WinAPI.FileMapping .OpenFileMapping((int)WinAPI.FileMapping .MapProtection.PageReadWrite, false, MappingFileName);
            fileHandle = WinAPI.FileMapping.MapViewOfFile(mappingHandle, (int)WinAPI.FileMapping.MapAccess.FileMapRead, 0, 0, new IntPtr(1024));
            //Marshal.GetLastWin32Error();
            
            Marshal.Copy(fileHandle, msg, 0, 1024);

            message=  Encoding.ASCII.GetString(msg);
            WinAPI.FileMapping.UnmapViewOfFile(fileHandle);
            WinAPI.Process.CloseHandle(fileHandle);
            WinAPI.Process.CloseHandle(mappingHandle);

            return message;
        }

    }

    public  static partial class Security
    {
        
        public static IntPtr GetSA()
        { 
            //System .Security
            return IntPtr.Zero;
        }
        public  static IntPtr ptr;
/*
        struct SECURITY_ATTRIBUTES
        {
            long  nLength;
            IntPtr lpSecurityDescriptor;
            bool bInheritHandle;

        }
 */
        /*
        struct SECURITY_DESCRIPTOR
        { 
        
        }
         */

    }
}